package com.sh.domain.demo.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.sh.config.ApplicationContextUtil;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hibernate.SessionFactory;
import org.hibernate.engine.spi.SessionImplementor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sh.common.base.BaseController;
import com.sh.common.constant.GlobalMessage;
import com.sh.common.model.JsonModel;
import com.sh.domain.demo.api.DemoApi;
import com.sh.domain.demo.dto.DemoPageDto;
import com.sh.domain.demo.dto.DemoSaveDto;
import com.sh.domain.demo.service.DemoService;
import com.sh.util.ExcelUtil;

/**
 * 
 * NOTE： DemoController
 *
 * @author： king
 * @date： 2019年4月2日
 *
 */
@RestController
public class DemoController extends BaseController implements DemoApi {

	@Autowired
	private DemoService demoService;
	
	@Override
	public JsonModel findDemoByPage(@RequestBody DemoPageDto demoDto) {
		
		JsonModel json = new JsonModel();
		
		json.setData(demoService.findDemoByPage(demoDto));
		
		return json;
	}

	@Override
	public JsonModel findDemoById(@RequestBody String id) {
		
		JsonModel json = new JsonModel();
		
		json.setData(demoService.findDemoById(id));
		
		return json;
	}
	
	@Override
	public JsonModel save(@RequestBody DemoSaveDto demoSaveDto) {
		
		JsonModel json = new JsonModel();
		
		demoService.save(demoSaveDto);
		json.setCode(GlobalMessage.SAVE_SUCCESS.code);
		json.setMessage(GlobalMessage.SAVE_SUCCESS.i18n);
		
		return json;
	}

	@Override
	public JsonModel update(@RequestBody DemoSaveDto demoSaveDto) {

		JsonModel json = new JsonModel();
		
		demoService.update(demoSaveDto);
		json.setCode(GlobalMessage.SAVE_SUCCESS.code);
		json.setMessage(GlobalMessage.SAVE_SUCCESS.i18n);
		
		return json;
	}

	@Override
	public JsonModel delete(@RequestBody String id) {

		JsonModel json = new JsonModel();
		
		demoService.delete(id);
		json.setCode(GlobalMessage.DELETE_SUCCESS.code);
		json.setMessage(GlobalMessage.DELETE_SUCCESS.i18n);
		
		return json;
	}

	@Override
	public JsonModel testExcel() {
		
		JsonModel json = new JsonModel();
		//
		SessionFactory sessionFactory =(SessionFactory) ApplicationContextUtil.getBean("sessionFactory");
		SessionImplementor session = sessionFactory.createEntityManager().unwrap(SessionImplementor.class);
		Connection connection = session.connection();
		try {
			Statement statement = connection.createStatement();
			String sql="select * from user";
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()){
				System.out.println(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		try {
			
			// excel 读取
			String path = "F:/test/201903月份云上酒店管理系统测试.xlsx";
			ExcelUtil excelRead = new ExcelUtil(new File(path));
			excelRead.setPattern("yyyy-MM-dd HH:mm");
			
			List<List<String>> list = excelRead.read(0);
			
			for(List<String> children : list) {
				
				for(String value : children) {
					
					System.out.print("="+ value +"=");
				}
				System.out.println("");
			}
			
			// excel 导出
			String newPath = "F:/test/test.xlsx";
			Workbook workbook = new XSSFWorkbook();
			ExcelUtil excelWrite = new ExcelUtil(workbook);
			excelWrite.write(list, "导出信息", true);
			FileOutputStream output = new FileOutputStream(newPath);
			workbook.write(output);
			output.flush();
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return json;
	}

	@Override
	public JsonModel testUpload(@RequestParam("file")MultipartFile file, @RequestParam("entity") String entity) {
		
		JsonModel json = new JsonModel();
		
		System.out.println(file.getOriginalFilename());
		
		try {
			
			String filePath = "F:/test/" + file.getOriginalFilename();
			FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return json;
	}
}
